<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 *
 * Sets up basic model functions. All user created model classes should
 * extend this to gain access to its basic database model functions.
 *
 * @package		YDD
 * @subpackage	Models
 * @author		hyw,fjm,cgm
 * @copyright	Copyright (c) 2010, ygkjw.com, Inc.
 * @since		Version 1.0
 */
class Base_model extends CI_Model
{
	protected $_db_name = '';		//数据库名
    protected $_table_name = ''; 	//表名
    protected $_fields = NULL; //Fields to return from table
    protected $_protect_fields = TRUE;
    protected $_id;
    protected $_info;
	protected $_dbs;


    public function __construct()
    {
        parent::__construct();
		
		if($this->_db_name == '')
		{
			$this->_dbs = &$this->db;
		}
		else
		{
			$this->_dbs = $this->load->database($this->_db_name, TRUE);
		}
    }

    protected function result()
    {
        $query = $this->_dbs->get($this->_table_name);
        $data_arr = $query->result();

        //清空缓存
        $query->free_result();
        //$query_total->free_result();

        return $data_arr;
    }

    protected function result_rows()
    {
    	$query = $this->_dbs->get($this->_table_name);
    	$data_arr = $query->num_rows();
    	$query->free_result();
    	return $data_arr;
    }

    protected function all()
    {
    	$data_arr = array();
        $query = $this->_dbs->get($this->_table_name);
        $data_arr = $query->result();

        //清空缓存
        $query->free_result();
        return $data_arr;
    }

    protected function row($n = 0)
    {
        $query = $this->_dbs->get($this->_table_name);
        return $query -> row($n);
    }

    //添加一条数据
    protected function insert($data_arr = NULL)
    {
        if($this->_dbs->insert($this->_table_name,$data_arr))
        {
        	return $this->_dbs->insert_id();
        }
        return false;
    }

    //添加多条数据
    protected function insert_batch($data_arr = NULL)
    {
        $this->_dbs->insert_batch($this->_table_name,$data_arr);
        return $this->_dbs->insert_id();
    }

    //删除
    protected function delete($where = NULL)
    {
        return $this->_dbs->delete($this->_table_name,$where);
    }

    //
    protected function update($data = NULL,$where = NULL)
    {
        return $this->_dbs->update($this->_table_name, $data,$where);
    }

    //第一个参数为要更新的表名，第二个参数为要更新的数据，是个二维数组，第三个 参数是 WHERE 语句的键
    protected function update_batch($data = NULL,$where = NULL)
    {
    	return $this->_dbs->update_batch($this->_table_name, $data,$where);
    }


    //add和update
    protected function replace($data = NULL)
    {
        return $this->_dbs->replace($this->_table_name,$data);
    }


	/**
     * set_limit
     *
     * @access public
     * @param array $limit 当$limit为“count”时返回当前的总行数，为$limit ='3,2'时显示3行偏移2行开始limit 2,3
     */
    public function set_limit($limit = NULL)
    {
        if ($limit != NULL)
        {
            $limit_array = explode(",", $limit);
            $rows = intval($limit_array['0']); //1
            $offset = isset($limit_array['1']) ? intval($limit_array['1']) : 0;//10
            $this->_dbs->limit($rows, $offset);
			return false;
        }
		else
		{
			return true;
		}
    }

    

    public function set_or_like($key,$val) {
		$arr = explode(',', $key);
		$where ="";
		foreach($arr as $_val){
			if($where=="")
				$where= $_val." LIKE '%".$val."%'";
			else 
				$where.=" OR ".$_val." LIKE '%".$val."%'";
		}
		$where="(".$where.")";
		$this->_dbs->where($where);
	}

    /**
	 * WHERE
	 *
	 * Generates the WHERE portion of the query.
	 * Separates multiple calls with 'AND'.
	 *
	 * @param	mixed
	 * @param	mixed
	 * @param	bool
	 * @return	CI_DB_query_builder
	 */
	protected function where($key, $value = NULL, $escape = NULL)
	{
		return $this->_dbs->where($key, $value, $escape);
	}

	// --------------------------------------------------------------------

	/**
	 * JOIN
	 *
	 * Generates the JOIN portion of the query
	 *
	 * @param	string
	 * @param	string	the join condition
	 * @param	string	the type of join
	 * @param	string	whether not to try to escape identifiers
	 * @return	CI_DB_query_builder
	 */
	protected function join($table, $cond, $type = '', $escape = NULL)
	{
		return $this->_dbs->join($table,$cond,$type,$escape);
	}
	
	/**
	 * ORDER BY
	 *
	 * @param	string	$orderby
	 * @param	string	$direction	ASC, DESC or RANDOM
	 * @param	bool	$escape
	 * @return	CI_DB_query_builder
	 */
	protected function order_by($orderby, $direction = '', $escape = NULL)
	{
		return $this->_dbs->order_by($orderby,$direction,$escape);
	}
	
	/**
	 * WHERE IN
	 *
	 * Generates a WHERE field IN('item', 'item') SQL query,
	 * joined with 'AND' if appropriate.
	 *
	 * @param	string	$key	The field to search
	 * @param	array	$values	The values searched on
	 * @param	bool	$escape
	 * @return	CI_DB_query_builder
	 */
	protected function where_in($key = NULL, $values = NULL, $escape = NULL)
	{
		return $this->_dbs->where_in($key, $values, $escape);
	}
	
	/**
	 * OR WHERE
	 *
	 * Generates the WHERE portion of the query.
	 * Separates multiple calls with 'OR'.
	 *
	 * @param	mixed
	 * @param	mixed
	 * @param	bool
	 * @return	CI_DB_query_builder
	 */
	protected function or_where($key, $value = NULL, $escape = NULL)
	{
		return $this->_dbs->or_where( $key, $value, $escape);
	}
	
	/**
	 * Select
	 *
	 * Generates the SELECT portion of the query
	 *
	 * @param	string
	 * @param	mixed
	 * @return	CI_DB_query_builder
	 */
	protected function select($select = '*', $escape = NULL)
	{
		return $this->_dbs->select( $select, $escape);
	}

}
/* End of file base_model.php */
/* Location: ./system/application/models/base_model.php */
